<?php
/*=============================================*/
/* index.php                         */
/* ------------------------------------------- */
/* lists all projects and presents options     */
/* to modify them. all mods are through        */
/* modify_projects.php						   */
/*=============================================*/

$maint_log_subhead = $maint_log_title = "List Projects";
require_once("include_functions.php");
require_once("include_header.php");

?>
<div id="content">
<!-- begin project list filter -->
		<script type="text/javascript">
			window.addEvent('domready', function(){
				var status = {
					'true': 'Hide',
					'false': 'Show'
				};
<?php
//if a filter is set, we should just show the filters right away
	if(isset($_POST['filter'])) {
		echo "var mySlide = new Fx.Slide('proj-list-filter');";
	} else {
		echo "var mySlide = new Fx.Slide('proj-list-filter').hide();";
	}
?>
				$('toggle-filters').addEvent('click', function(e){
					e.stop();
					mySlide.toggle();
				});
				mySlide.addEvent('complete', function() {
					$('visible_status').set('text', status[mySlide.open]);
				});
			});
		</script>
<!-- filter slide toggle button -->
		<a class="nav button" href="#" id="toggle-filters">
			<span class="button">
				<span id="visible_status">
					<?php echo (isset($_POST['filter'])) ? 'Hide' : 'Show';?>
				</span> Filters
			</span>
		</a>
<!-- filter slide element -->
		<div id="proj-list-filter">
			<form action="index.php" method="POST">
				Order By: 
				<select name="orderBy">
<?php
	$orderByOptions = array('projRequestedDate' => 'Requested Completion Date',
							'workerAssignedId'  => 'Worker Assigned',
							'projPriority'		=> 'Project Priority');
	foreach($orderByOptions as $value => $text) {
		if(isset($_POST['orderBy']) && $_POST['orderBy'] == $value) {
			echo '<option value="'. $value .'" selected="selected">'. $text .'</option>';
		} else {
			echo '<option value="'. $value .'">'. $text .'</option>';
		}
	}
?>
				</select>
				Show Completed:
				<input type="checkbox" name="showCompleted" <?php if(isset($_POST['showCompleted']) && $_POST['showCompleted'] == 'on') echo 'checked="checked"';?> />
				<input type="submit" name="filter" value="Filter" />
			</form>
		</div>
<!-- end project list filter -->
<!-- begin project list -->
<?php
	//check if a filter has been set
	if(isset($_POST['filter'])) {
		// should we show completed projects
		if (isset($_POST['showCompleted']) && $_POST['showCompleted'] == 'on') {
			$sql =	"SELECT projects.*, workers.workerName ".
				"FROM projects ".
				"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
				"ORDER BY projects.". clean($_POST['orderBy']) .";";
		} else {
			$sql =	"SELECT projects.*, workers.workerName ".
				"FROM projects ".
				"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
				"WHERE projects.projCompletedDate IS NULL ".
				"ORDER BY projects.". clean($_POST['orderBy']) .";";
		}
	} else {
	// if no filter or order by is set then just query everything and
	// order by the default (requested by date, completed projects not shown)
	$sql =	"SELECT projects.*, workers.workerName ".
		"FROM projects ".
		"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
		"WHERE projects.projCompletedDate IS NULL ".
		"ORDER BY projects.projRequestedDate, projects.projCreatedDate;";
	}
	if(!$result = mysql_query($sql)) echo mysql_error();
	if(mysql_num_rows($result) == 0) {
?>
		<div class="proj-row">
			<div class="proj-title">No projects</div>
		</div>
<?php
	}
	while($proj = mysql_fetch_array($result, MYSQL_ASSOC)) {
?>

		<div class="proj-row">
			<div class="proj-title"><a href="project.php?projId=<?php echo $proj['projId'];?>"><?php echo $proj['projName'];?></a>
				<span class="proj-worker">&nbsp;-&nbsp;<?php echo $proj['workerName'];?></span>
				<span class="proj-controls">
					<a href="manage_projects.php?action=begin&projId=<?php echo $proj['projId'];?>" title="Begin Project">
						<img src="img/wrench.png">
					</a>
					<a href="edit_proj.php?projId=<?php echo $proj['projId'];?>" title="Edit Project">
						<img src="img/pencil.png">
					</a>
					<a href="manage_projects.php?action=complete&projId=<?php echo $proj['projId'];?>" title="Mark as Complete">
						<img src="img/accept.png">
					</a>
					<a href="manage_projects.php?action=delete&projId=<?php echo $proj['projId'];?>" title="Delete Project">
						<img src="img/cancel.png">
					</a>
				</span>
			</div>
			<div class="proj-desc">
				<span class="proj-dates">
					Project Created:<?php echo $proj['projCreatedDate'];?><br />
					Requested By:<?php echo $proj['projRequestedDate'];?><br />
					Started On:<?php echo $proj['projStartedDate'];?><br />
					Completed On:<?php echo $proj['projCompletedDate'];?>
				</span><?php echo substr($proj['projDescription'], 0, 550);?>
			</div>
		</div>
<?php
	}
?>
<!-- end project list -->
</div>

<?php
require_once("include_footer.php");
?>
